package com.zl.roomdesignserver.service;

import com.zl.roomdesignserver.entity.auth.User;
import com.zl.roomdesignserver.entity.design.MaterialEntity;
import com.zl.roomdesignserver.entity.design.MaterialKindEntity;
import com.zl.roomdesignserver.entity.design.UploadFileEntity;
import com.zl.roomdesignserver.exception.ControllerException;
import com.zl.roomdesignserver.repository.AuthRepository;
import com.zl.roomdesignserver.repository.DesignRepository;
import com.zl.roomdesignserver.repository.MaterialKindRepository;
import com.zl.roomdesignserver.repository.MaterialRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Created by zhangli on 2018/5/5 14:09.<br/>
 */
@Service
public class DesignService {

    @Autowired
    DesignRepository designRepository;

    @Autowired
    MaterialRepository materialRepository;

    @Autowired
    MaterialKindRepository materialKindRepository;

    public UploadFileEntity save(UploadFileEntity entity) {
        return designRepository.save(entity);
    }

    public List<UploadFileEntity> getAll() {
        return designRepository.findAll();
    }


    public List<UploadFileEntity> getByUser(long phone) {

        //创建查询条件数据对象
        UploadFileEntity customer = new UploadFileEntity();
        customer.setPhone(phone);


        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withIgnoreNullValues();

        Example<UploadFileEntity> ex = Example.of(customer, matcher);
        return designRepository.findAll(ex);
    }

    public List<MaterialEntity> getAllMaterial() {
        List<MaterialEntity> all = materialRepository.findAll();

        Map<MaterialEntity, List<MaterialEntity>> map = new HashMap<>();

        for (MaterialEntity entity : all) {
            if (map.containsKey(entity)) {
                map.get(entity).add(entity);
            } else {
                List<MaterialEntity> list = new ArrayList<>();
                list.add(entity);
                map.put(entity, list);
            }
        }
        List<MaterialEntity> result = new ArrayList<>();
        Set<Map.Entry<MaterialEntity, List<MaterialEntity>>> set = map.entrySet();
        for (Map.Entry<MaterialEntity, List<MaterialEntity>> entry : set) {
            MaterialEntity key = entry.getKey();
            MaterialEntity entity = new MaterialEntity();
            entity.setType(key.getType());
            entity.setName(key.getType());
            entity.setFileName(key.getFileName());
            entity.setList(entry.getValue());
            result.add(entity);
        }
        return result;
    }

    public void insertAll(List<MaterialEntity> list){
        materialRepository.deleteAll();
        materialRepository.saveAll(list);

        materialKindRepository.deleteAll();
        HashSet<MaterialKindEntity> set = new HashSet<>();
        for (MaterialEntity entity : list) {
            MaterialKindEntity kindEntity = new MaterialKindEntity();
            kindEntity.setType(entity.getType());
            kindEntity.setName(entity.getType());
            kindEntity.setTimeStamp(System.currentTimeMillis()/1000);
            set.add(kindEntity);
        }
        materialKindRepository.saveAll(set);
    }

    public void saveMaterial(MaterialEntity entity) {
        materialRepository.save(entity);
    }

    public void delete(String id) {
        designRepository.deleteById(Long.parseLong(id));
    }
}
